package com.analysis.apresunday.controller;


import com.analysis.apresunday.config.R;
import com.analysis.apresunday.entity.dto.UserLoginDTO;
import com.analysis.apresunday.entity.dto.UserRegisterDTO;
import com.analysis.apresunday.entity.dto.UserUpdateDTO;
import com.analysis.apresunday.entity.vo.UserInfoVo;
import com.analysis.apresunday.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author snake_skin
 * @since 2024-01-23
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Tag(name = "用户接口")
@Slf4j
public class UserController {

    @Resource
    UserService userService;

    @Operation(summary = "注册")
    @PostMapping("signUp")
    public R save(@RequestBody UserRegisterDTO userRegisterDto) {
        log.info("注册数据：{}",userRegisterDto);
        userService.register(userRegisterDto);
        return R.ok();
    }

    @Operation(summary = "登录获取token")
    @PostMapping("signIn")
    public R login(@RequestBody UserLoginDTO userLoginDTO) {
        String token = userService.login(userLoginDTO);
        return R.ok().data("token",token);
    }

    @Operation(summary = "登录后通过token获取用户信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        UserInfoVo user = userService.getLoginInfoByToken(request);
        return R.ok().data("user",user);
    }

    @Operation(summary = "后台-获取所有用户信息")
    @GetMapping("/back/getUserList")
    public R getUserList() {
        return R.ok().data("userList",userService.list());
    }

    @Operation(summary = "后台-id删除用户信息")
    @DeleteMapping("/back/{userId}")
    public R deleteUser(@PathVariable String userId) {
        userService.removeById(userId);
        return R.ok();
    }

    @Operation(summary = "后台-更新用户信息")
    @PutMapping("/back")
    public R deleteUser(@RequestBody UserUpdateDTO userUpdateDto) {
        log.info("更新用户信息：{}",userUpdateDto);
        userService.updateUser(userUpdateDto);
        return R.ok();
    }

}
